package com.xc.hotelmgr.datapusherali.service.mybatis.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.hotelmgr.datapusherali.mapper.HtPhysicalRoomMapper;
import com.xc.hotelmgr.datapusherali.pojo.ali.room.S_roomtype;
import com.xc.hotelmgr.datapusherali.pojo.ali.room.XhotelRoomType;
import com.xc.hotelmgr.datapusherali.pojo.mybatis.HtPhysicalRoom;
import com.xc.hotelmgr.datapusherali.sdk.FzSdk;
import com.xc.hotelmgr.datapusherali.service.mybatis.HtPhysicalRoomService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HtPhysicalRoomServiceImpl extends ServiceImpl<HtPhysicalRoomMapper, HtPhysicalRoom> implements HtPhysicalRoomService {

    @Autowired
    private HtPhysicalRoomMapper htPhysicalRoomMapper;

    @Autowired
    private FzSdk fzSdk;

    @Override
    public int updateExceptNull(HtPhysicalRoom htPhysicalRoom) {
        return htPhysicalRoomMapper.updateIgnoreNull(htPhysicalRoom);
    }

    @Override
    public List<HtPhysicalRoom> listAll() {
        return null;
    }

    @Override
    public HtPhysicalRoom getById(Integer id) {
        return null;
    }

    @Override
    public int insert(HtPhysicalRoom htPhysicalRoom) {
        return 0;
    }

    @Override
    public int insertIgnoreNull(HtPhysicalRoom htPhysicalRoom) {
        return 0;
    }

    @Override
    public int update(HtPhysicalRoom htPhysicalRoom) {
        return 0;
    }

    @Override
    public int delete(HtPhysicalRoom htPhysicalRoom) {
        return 0;
    }

    @Override
    public void roomUpdateByAli(String roomResult) {
        XhotelRoomType xhRommType = JSONObject.parseObject(roomResult,XhotelRoomType.class);
        HtPhysicalRoom htPhysicalRoom = new HtPhysicalRoom();
        htPhysicalRoom.setId(Long.valueOf(xhRommType.getXhotel_roomtype_add_response().getXroomtype().getOuter_id()));
        Long srid = 1L;
        String roomName = "随机房型";
        htPhysicalRoom.setMatchStatus(xhRommType.getXhotel_roomtype_add_response().getXroomtype().getMatch_status());
        S_roomtype sRoomtype = xhRommType.getXhotel_roomtype_add_response().getXroomtype().getS_roomtype();
        if (sRoomtype != null){
            if(sRoomtype.getSrid() != null){
                srid = sRoomtype.getSrid();
            }
            if(StringUtils.isNotBlank(sRoomtype.getName())){
                roomName = sRoomtype.getName();
            }
            if(srid != 1L){
                htPhysicalRoom.setSrid(srid);
            }
            if(srid == 1L || roomName.contains("随机房型") || roomName.contains("其他")){
                htPhysicalRoom.setMatchStatus(2);
            }else {
                htPhysicalRoom.setRoomName(roomName);
//                if(StringUtils.isNotBlank(sRoomtype.getBed()) && StringUtils.isNotBlank(sRoomtype.getBed().replace("[]",""))){
////                    htPhysicalRoom.setBedPad(sRoomtype.getBed());
////              }
                if(StringUtils.isNotBlank(sRoomtype.getArea())){
                    htPhysicalRoom.setRoomSize(sRoomtype.getArea());
                }
                if(StringUtils.isNotBlank(sRoomtype.getFloor())){
                    htPhysicalRoom.setRoomFloor(sRoomtype.getFloor());
                }
            }
            try{
                updateById(htPhysicalRoom);
            }catch (Exception ex){
                log.error("阿里返回更新物理房型失败："+ ex.toString());
            }
        }

    }
}
